package com.kly.user.rulePolicy;

import com.fqgj.exception.common.ApplicationException;
import com.kly.user.rulePolicy.enums.VariableTypeEnum;
import java.util.Objects;

/**
 * 规则计算类
 * lt    (less than)                    小于
 * le （less than or equal to)          小于等于
 * eq  (equal to)                       等于
 * ne  (not equal to)                   不等于
 * ge  (greater than or equal to)       大于等于
 * gt   (greater  than)                 大于
 * @author linqu
 */
public class Utils {

    /**
     * 大于
     * @param a1
     * @param a2
     * @return
     */
    public static Boolean gt(Variable a1, Variable a2) {
        validVariable(a1);
        validVariable(a2);
        if (VariableTypeEnum.INT.name().equals(a1.getVarType()) && VariableTypeEnum.INT.name().equals(a2.getVarType())) {
            return Integer.parseInt(a1.getValue()) > Integer.parseInt(a2.getValue());
        }
        if (VariableTypeEnum.DOUBLE.name().equals(a1.getVarType()) && VariableTypeEnum.DOUBLE.name().equals(a2.getVarType())) {
            return Double.parseDouble(a1.getValue()) > Double.parseDouble(a2.getValue());
        }
        throw new ApplicationException("参与计算的必须为数值类型的参数");

    }

    /**
     * 大于等于
     * @param a1
     * @param a2
     * @return
     */
    public static Boolean ge(Variable a1, Variable a2) {
        validVariable(a1);
        validVariable(a2);
        if (VariableTypeEnum.INT.name().equals(a1.getVarType()) && VariableTypeEnum.INT.name().equals(a2.getVarType())) {
            return Integer.parseInt(a1.getValue()) >= Integer.parseInt(a2.getValue());
        }
        if (VariableTypeEnum.DOUBLE.name().equals(a1.getVarType()) && VariableTypeEnum.DOUBLE.name().equals(a2.getVarType())) {
            return Double.parseDouble(a1.getValue()) >= Double.parseDouble(a2.getValue());
        }
        throw new ApplicationException("参与计算的必须为数值类型的参数");

    }

    /**
     * 小于
     * @param a1
     * @param a2
     * @return
     */
    public static Boolean lt(Variable a1, Variable a2) {
        validVariable(a1);
        validVariable(a2);
        if (VariableTypeEnum.INT.name().equals(a1.getVarType()) && VariableTypeEnum.INT.name().equals(a2.getVarType())) {
            return Integer.parseInt(a1.getValue()) < Integer.parseInt(a2.getValue());
        }
        if (VariableTypeEnum.DOUBLE.name().equals(a1.getVarType()) && VariableTypeEnum.DOUBLE.name().equals(a2.getVarType())) {
            return Double.parseDouble(a1.getValue()) < Double.parseDouble(a2.getValue());
        }
        throw new ApplicationException("参与计算的必须为数值类型的参数");

    }

    /**
     * 小于等于
     * @param a1
     * @param a2
     * @return
     */
    public static Boolean le(Variable a1, Variable a2) {
        validVariable(a1);
        validVariable(a2);
        if (VariableTypeEnum.INT.name().equals(a1.getVarType()) && VariableTypeEnum.INT.name().equals(a2.getVarType())) {
            return Integer.parseInt(a1.getValue()) <= Integer.parseInt(a2.getValue());
        }
        if (VariableTypeEnum.DOUBLE.name().equals(a1.getVarType()) && VariableTypeEnum.DOUBLE.name().equals(a2.getVarType())) {
            return Double.parseDouble(a1.getValue()) <= Double.parseDouble(a2.getValue());
        }
        throw new ApplicationException("参与计算的必须为数值类型的参数");

    }

    /**
     * 等于
     * @param a1
     * @param a2
     * @return
     */
    public static Boolean eq(Variable a1, Variable a2) {
        validVariable(a1);
        validVariable(a2);
        if (VariableTypeEnum.INT.name().equals(a1.getVarType()) && VariableTypeEnum.INT.name().equals(a2.getVarType())) {
            return Integer.parseInt(a1.getValue()) == Integer.parseInt(a2.getValue());
        }
        if (VariableTypeEnum.DOUBLE.name().equals(a1.getVarType()) && VariableTypeEnum.DOUBLE.name().equals(a2.getVarType())) {
            return Double.parseDouble(a1.getValue()) == Double.parseDouble(a2.getValue());
        }
        throw new ApplicationException("参与计算的必须为数值类型的参数");

    }

    public static void validVariable(Variable var) {

        if (Objects.isNull(var)) {
            throw new ApplicationException("参数不能为空");
        }
        if (Objects.isNull(VariableTypeEnum.transformDataName(var.getVarType()))) {
            throw new ApplicationException("非法的参数类型");
        }
    }
}
